<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Account extends CI_Controller {
    /* PUBLIC METHODS */

    public function __construct() {
        parent::__construct();

        //$this->load_construct->set_construct();
    }

    public function index() {
        if ($this->session->userdata('USR_ID') == "")
            page('authentification', 'Authentification', null);
        else
            page('profil', 'Profil', null);
    }

    /* PAGES */

    public function login() {
        // Validation du formulaire.
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('login', '"Identifiant"', 'trim|required|min_length[3]|max_length[52]|encode_php_tags|xss_clean');
        $this->form_validation->set_rules('password', '"Mot de passe"', 'trim|required|min_length[3]|max_length[52]|encode_php_tags|xss_clean');

        $login = $this->input->post('login');
        $password = $this->input->post('password');

        // Si le formulaire est posté
        if (false !== $login && false !== $password) {

            // On test de connecter...
            $datas = array(
                '\'' . $login . '\'',
                '\'' . $password . '\'',
            );
            $result = callProc('DB_PLASTPROD', 'S_BUSINESS_DATA', 'P_S_USER_LOGIN_00', $datas);

            // On regarde si l'identification est possible.
            if ('SUCCESS' === $result->RESULT) {
                $fields = array(
                    '"DB_PLASTPROD."S_BUSINESS_DATA"."T_D_USER_USR"."USR_ID"',
                    '"DB_PLASTPROD."S_BUSINESS_DATA"."T_D_USER_USR"."USR_FIRST_NAME"',
                    '"DB_PLASTPROD."S_BUSINESS_DATA"."T_D_USER_USR"."USR_LAST_NAME"',
                    '"DB_PLASTPROD."S_BUSINESS_DATA"."T_D_USER_USR"."USR_EMAIL"'
                );

                $this->db = $this->load->database('DB_PLASTPROD', true);
                $tmp = $this->db
                        ->select(implode(', ', $fields)) 
                        ->from('"DB_PLASTPROD."S_BUSINESS_DATA"."T_D_USER_USR"')
                        ->where('"DB_PLASTPROD."S_BUSINESS_DATA"."T_D_USER_USR"."USR_ID"', $result->MESSAGE)
                        ->get()
                        ->result()
                ;
                $user = $tmp[0];

                $this->session->set_userdata($user);

                redirect('main/home');
            }

            // Si l'identification n'est pas possible.
            else {
                // Message d'erreur.
                $datas = array(
                    'alertType' => 'error',
                    'alertText' => $this->_loginFormatError($result->MESSAGE)
                );

                $this->layout
                        ->setTitle($this->_getTitle())
                        ->view('account/login', $datas)
                        ->display()
                ;
            }
        }


        // Si le formulaire n'est pas posté.
        else {
            $output = array();
            $this->layout
                    ->setTitle($this->_getTitle())
                    ->view('account/login', $output)
                    ->display()
            ;
        }


        /* if ($this->form_validation->run()) {



          var_dump('kikou');
          return;
          // Loading Model
          $this->load->model('authentification_model');

          // We define the parameters users with login (utilisateur_id, societe_id, utilisateur_nom, utilisateur_prenom, utilisateur_email, utilisateur_password, utilisateur_actif)
          $data = $this->authentification_model->get_authentification();

          if (!empty($data)) {
          // We apply the settings to the current session
          $this->session->set_userdata($data);

          //$_SESSION['utilisateur_id'] = $data['utilisateur_id'];


          redirect('Profil');
          } else {

          // Display an error message
          $data = array(
          'alertType' => 'error',
          'alertText' => 'Identifiant ou mot de passe incorrect !'
          );


          redirect('account/login');
          //                page('account', 'Authentification', $data);
          }
          } else {
          // Initialization of session variables
          $this->session->sess_destroy();

          // Incorrect form, the user must re-enter their username and password
          //$this->template->load('template', 'login');
          //page('account', 'login', null);
          }


          $output = array();
          $this->layout
          ->setTitle($this->_getTitle())
          ->view('account/login', $output)
          ->display()
          ; */
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect('account/login');
    }
    
    private function _loginFormatError($error) {
        switch ($error) {
            case 'USER NOT EXIST':
                $toReturn = "l\'utilisateur n\'existe pas.";
                break;

            case 'USER IS DISABLE':
                $toReturn = "l\'utilisateur est desactivé.";
                break;

            case 'USER BAD PASSWORD':
                $toReturn = "Mot de passe invalide.";
                break;

            default:
                $toReturn = "Erreur indéfinie.";
                break;
        }
        return $toReturn;
    }

    private function _getTitle() {
        $action = $this->uri->segment(2);

        switch ($action) {
            case 'login':
                return 'Identification';
                break;

            default:
                return '';
                break;
        }
    }

}